/*
 * li_bitmap.h
 *
 *  Created on: 2012-3-25
 *      Author: zgx
 */

#ifndef LI_BITMAP_H_
#define LI_BITMAP_H_

#pragma pack(push, 1)

typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;

typedef struct tagBITMAPFILEHEADER {

    U16 bfType;
    U32 bfSize;
    U16 bfReserved1;
    U16 bfReserved2;
    U32 bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {
    U32 biSize;
    U32 biWidth;
    U32 biHeight;
    U16 biPlanes;
    U16 biBitCount;
    U32 biCompression;
    U32 biSizeImage;
    U32 biXPelsPerMeter;
    U32 biYPelsPerMeter;
    U32 biClrUsed;
    U32 biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagRGBQUAD {
    U8 rgbBlue;
    U8 rgbGreen;
    U8 rgbRed;
    U8 rgbReserved;
} RGBQUAD;

typedef struct tagBITMAPINFO {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD bmiColors[1];
} BITMAPINFO;

typedef struct tagBITMAP {
    BITMAPFILEHEADER bfHeader;
    BITMAPINFO biInfo;
} BITMAPFILE;

#define RGB565_MASK_RED        0xF800
#define RGB565_MASK_GREEN      0x07E0
#define RGB565_MASK_BLUE       0x001F
static void rgb565_2_rgb24(U8 *rgb24, U16 rgb565) //把一个字的565数据转成3个分离的R ,G, B
{
    //extract RGB
    //分离出单独的RGB
    rgb24[2] = (rgb565 & RGB565_MASK_RED) >> 11;

    rgb24[1] = (rgb565 & RGB565_MASK_GREEN) >> 5;

    rgb24[0] = (rgb565 & RGB565_MASK_BLUE);

    //amplify the image
    //往高位移动填满单字节的8位
    rgb24[2] <<= 3;
    rgb24[1] <<= 2;
    rgb24[0] <<= 3;
}

static U16 rgb24_2_rgb565(int r, int g, int b) //应该会损失数据内容
{
    return (U16) ((((U16) (r) << 8) & 0xF800) | (((U16) (g) << 3) & 0x7E0)
            | (((U16) (b) >> 3)));
}

int SaveRGB565_BmpFile(U8 *pData, U32 dataLen, int width, int height,
        const char *filename);

#pragma pack(pop)

#endif /* LI_BITMAP_H_ */
